package com.example.bishetianqi;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.bishetianqi.db.DBManager;
import com.example.bishetianqi.db.DatabaseBean;

import java.util.ArrayList;
import java.util.List;

public class CityManagerActivity extends AppCompatActivity implements View.OnClickListener{

    ImageView add,delete,back;
    ListView cityLV;
    List<DatabaseBean>databaseBeanList;
    private CityManagerAdapter cityManagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        setContentView(R.layout.activity_city_manager);
        add=findViewById(R.id.city_iv_add);
        back=findViewById(R.id.city_iv_back);
        delete=findViewById(R.id.city_iv_delete);
        cityLV=findViewById(R.id.city_lv);
        databaseBeanList=new ArrayList<>();

        //添加点击事件
        add.setOnClickListener(this);
        back.setOnClickListener(this);
        delete.setOnClickListener(this);

        //添加适配器
        cityManagerAdapter = new CityManagerAdapter(this, databaseBeanList);
        cityLV.setAdapter(cityManagerAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        List<DatabaseBean> list = DBManager.queryAllInfo();
        databaseBeanList.clear();
        databaseBeanList.addAll(list);
        cityManagerAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.city_iv_add:
                int cityCount = DBManager.getCityCount();
                if (cityCount<5) {
                    Intent intent = new Intent(this, SearchCityActivity.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(this,"存储城市数量已达上限，请删除后再增加",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.city_iv_back:
                finish();
                break;
            case R.id.city_iv_delete:
                Intent intent1=new Intent(this,DeleteCityActivity.class);
                startActivity(intent1);
                break;
        }
    }
}
